﻿using HCQ2_IBLL;
using HCQ2_Model.DocModel.APIParam;
using HCQ2_Model.DocModel.APIResultModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_BLL
{
    public partial class T_VRErrorWordBLL: IT_VRErrorWordBLL
    {
        /// <summary>
        ///  获取用户错误单词列表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public async Task<List<UserErrorList>> GetErrorWordByUser(ErrorWordParams model, int user_id)
        {
            List<ErrorWordResult> list = await DBSession.IT_VRErrorWordDAL.GetErrorWordByUser(model, user_id);
            List<UserErrorList> result = new List<UserErrorList>();
            var fileList = list.Select(s =>s.file_id).ToList();
            HashSet<int> hs = new HashSet<int>(fileList);//存储去除重复的file_id
            foreach(int file_id in hs)
            {
                UserErrorList user = new UserErrorList();
                List<ErrorCpName> _cpCell = new List<ErrorCpName>();
                //去除重复的关卡
                var cellList = list.Where(s => s.file_id == file_id).OrderBy(s=>s.cp_order).Select(s => s.cp_id).ToList();
                HashSet<int> cell = new HashSet<int>(cellList);
                foreach(int i in cell)
                {
                    ErrorCpName cpCell = new ErrorCpName();
                    List<ErrorWord> _word = new List<ErrorWord>();
                    cpCell.cp_name = list.Where(s => s.file_id == file_id && s.cp_id == i).FirstOrDefault()?.cp_name;//关卡名
                    //单词
                    var word = list.Where(s => s.file_id == file_id && s.cp_id == i).OrderBy(s => s.cp_order).ToList();
                    word.ForEach(o =>
                    {
                        user.file_name = o.file_name;
                        ErrorWord tempWord = new ErrorWord
                        {
                            word = o.word,
                            err_num = o.err_num,
                            num = o.num,
                            gj_symbol = o.gj_symbol,
                            cp_type = o.cp_type,
                            china_mean = o.china_mean,
                            isOk = o.isOk
                        };
                        _word.Add(tempWord);
                    });
                    cpCell.word = _word;
                    _cpCell.Add(cpCell);
                }
                user.cell = _cpCell;
                result.Add(user);
            }
            return result;
        }
    }
}
